package ModelLayer;
import java.util.ArrayList;
/**
 * 
 * @Author (Alexander Hvidberg, Dennis Kirkebak & Thomas Kulmbach)
 * @Team (Group 1)
 * @Version (08/11/13)
 * 
 */
public class DVD
{
    // instance variables - replace the example below with your own
    private int id;
    private String title;
    private String artist;
    private String releaseDate;
    private ArrayList<DVDArticle> articles;
    private ArrayList<DVDArticle> onloan;

    /**
     * Constructor for objects of class DVD
     * 
     * @param int id - the id
     * @String title - the title 
     * @param String artist - the artist(s)
     * @param String releaseDate - the release date
     * @param int serialNumber - the serialnumber
     * @param String aqDate - the acquiring date
     * @param double aqPrice  - the acquiring price
     */
    public DVD(int id, String title, String artist, String releaseDate,int serialNumber, String aqDate, double aqPrice)
    {
        this.id = id;
        this.title = title;
        this.artist = artist;
        this.releaseDate = releaseDate;
        articles = new ArrayList<DVDArticle>();
        DVDArticle a = new DVDArticle(serialNumber, aqDate, aqPrice); 
        articles.add(a);
        onloan = new ArrayList<DVDArticle>();
        
    }
    
    /**
     * Create a new DVDArticle and add it to this Arraylist.
     * 
     * @param int serialNumber - The serialnumber
     * @param String aqDate - acquiring date
     * @param double aqPrice - acquiring price 
     */
    public void createDVDArticle(int serialNumber, String aqDate, double aqPrice)
    {
        articles.add(new DVDArticle(serialNumber, aqDate, aqPrice)); 
    }
    
    /**
     * Update the information about the DVD.
     * 
     * @param String title, String artist, String releaseDate - 
     */
    public void updateDVD(String title, String artist, String releaseDate)
    {
        this.title=title;
        this.artist=artist;
        this.releaseDate=releaseDate;
    }
    
    /**
     * Find a DVDArticle in this DVD.
     * 
     * @param int serialNumber - The serialnumber of the article
     * 
     * @return DVDArticle a - The DVD -article with this serialnumber
     */
    public DVDArticle findDVDArticle(int serialNumber)
    {
        int index = 0;
        boolean found = false;
        DVDArticle a = null;
        while(index<articles.size() && !found) {
            if(serialNumber==articles.get(index).getSerialNumber()) {
                a = articles.get(index);
                found = true;
            }
            else {
                index++;
            }
        }
        return a;
    }
    
    /**
     * Get the size of the list articles
     * 
     * @return int size - the size of the articles list
     */
    public int getArticlesSize(){
        return articles.size();
    }
    
    /**
     * Rent an article. The article is moved from the articles-list to 
     * the onloan-list.
     * 
     * @ return int serialNumber/0 - returns the serialnumber of the article/copy
     */
    public int rentArticle()
    {
        int index = 0;
        boolean found = false;
        while(index<articles.size()) {
            DVDArticle a = articles.get(index);
            onloan.add(a);
            articles.remove(index);
            return a.getSerialNumber();
        }
        return 0;
    }
    
    /**
     * Mark the article as deposited. The article is moved back to 
     * the articles-list.
     * 
     * @param int articleNumber - the article-/serialnumber of the DVD
     */
    public void unrentArticle(int articleNumber)
    {
        int index = 0;
        boolean found = false;
        while(index<onloan.size() && !found) {
            if(articleNumber == onloan.get(index).getSerialNumber()) {
                DVDArticle a = onloan.get(index);
                articles.add(a);
                onloan.remove(index);
                found = true;           
            }
        }
        if(found == false) {
            System.out.println("\f  The copy of the dvd wasn't found. \n The dvd hasn't been returned. \n ");
        }
    }
    
    /**
     * Prints a list of the articles available for rent.
     */
    public void printArticlesList()
    {
        int index = 0;
        while(index<articles.size()) {
            System.out.println(index+1 +": "+articles.get(index).getSerialNumber());
        }
    }
    
    /**
     * Prints a list of the articles on loan.
     */
    public void printOnloanList()
    {
        int index = 0;
        while(index<onloan.size()) {
            System.out.println(index+1 +": "+onloan.get(index).getSerialNumber());
        }
    }
    /**
     * Remove a DVDArticle from this DVD.
     * 
     * @param int serialNumber - The serialnumber of the DVD-article that is to 
     * be deleted
     */
    public void deleteDVDArticle(int serialNumber)
    {
        DVDArticle a = findDVDArticle(serialNumber);
        articles.remove(a);
    }
    
    /**
     * Print short info about this DVD.
     */
    public void printDVDInfo()
    {
        System.out.println("ID: "+id);
        System.out.println("Title: "+title);
        System.out.println("Artist: "+artist);
        System.out.println("Releasedate: "+releaseDate);
    }  
    
    /**
     * Print all info about this dvd
     */
    public void printAllDVDInfo()
    {
        printDVDInfo();
        System.out.println("Available: "+articles.size());
        System.out.println("On loan: "+onloan.size()+ " \n " );
    }
    
    /**
     * Prints a list of articles on this dvd
     */
    public void printArticles()
    {
        System.out.println("DVD: "+title+ " \nCopies: ");
        for(DVDArticle article : articles) {
            System.out.println(article.getSerialNumber()+" \n");
        }
        for(DVDArticle loan : onloan) {
            System.out.println(loan.getSerialNumber()+" \n");
        }
    }
    //Set & Get methods for the entire class
    /**
     * Set the ID of  the dvd
     * 
     * @param int id - the ID of the dvd
     */
    public void setId(int id){
        this.id = id;
    }
    /**
     * set the title of the dvd
     * 
     * @param String title - The title of the dvd
     */
    public void settitle(String title){
         this.title = title;
    }
    /**
     * set the artist of the dvd
     * 
     * @param String artist - The artist of the dvd
     */
    public void setartist(String artist){
        this.artist = artist;
    }
    /**
     * Sets the releasedate of of the dvd
     * 
     * @param String releaseDate - the release date of the dvd
     */
    public void setreleaseDate(String releaseDate){
        this.releaseDate = releaseDate;
    }
    /**
     * Get the ID of this dvd
     * 
     * @return int id - the ID of the dvd
     */
    public int getId(){
        return id;
    }
    /**
     * Get the title of the dvd
     * 
     * @return String title - the title of the dvd
     */
    public String gettitle(){
        return title;
    }
    /**
     * Get the artist of the dvd
     * 
     * @return String artist - the artist of the dvd
     */
    public String getartist(){
        return artist;
    }
    /**
     * Get the release date of the dvd
     * 
     * @return String releaseDate - the release date of the dvd
     */
    public String getreleaseDate(){
        return releaseDate;
    }
}
